package cn.restcloud.springboot.listener;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import cn.restcloud.discovery.config.client.service.ClientInitLocalConfigFromCenter;

/**
 * springboot初始化时先执行配置数据的同步,所有配置数据同步远程配置中心后才能实例化本地的java bean配置
 * @author restcloud
 *
 */
public class RestCloudEnvPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>{
	
	public RestCloudEnvPreparedEventListener(){
		try {
			ClientInitLocalConfigFromCenter.init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
		//注册配置数据获取的数据源属性对像为restcloud的属性获取对像
        event.getEnvironment().getPropertySources().addFirst(new RestCloudConfigPropertySource());
    }
}
